package com.cn.kabalifeuser.ui.integral.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.CustomRefreshListener;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.config.RefreshHelper;
import com.cn.kabalifeuser.ui.integral.adapter.CommodityCommentListAdapter;
import com.cn.kabalifeuser.ui.integral.bean.CommodityCommentListBean;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.view.NotDataView;
import com.flyco.roundview.RoundTextView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/9/24.
 * function: 商品的评论
 */
public class CommodityCommentListActivity extends IBaseActivity<CommodityCommentListListView, CommodityCommentListPresenter> implements CommodityCommentListListView {
    @BindView(R.id.tv_btn_all)
    RoundTextView tvBtnAll;
    @BindView(R.id.tv_btn_hao)
    RoundTextView tvBtnHao;
    @BindView(R.id.tv_btn_zhong)
    RoundTextView tvBtnZhong;
    @BindView(R.id.tv_btn_cha)
    RoundTextView tvBtnCha;
    @BindView(R.id.rv_notice)
    RecyclerView rvNotice;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    Bundle bundle;
    int id;

    private View notDataView;
    List<CommodityCommentListBean.ResultBean.DataBean> mList;
    CommodityCommentListAdapter mAdapter;

    String type = "";

    @Override
    protected CommodityCommentListPresenter createPresenter() {
        return new CommodityCommentListPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_business_home_comments;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        setTitleBar("全部评论");

        bundle = getIntent().getExtras();
        id = bundle.getInt("id");

        mList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getMe());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        rvNotice.setLayoutManager(layoutManager);
        rvNotice.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new CommodityCommentListAdapter(R.layout.commodity_comment_item, mList, this);
        //给RecyclerView设置适配器
        rvNotice.setAdapter(mAdapter);
        notDataView = NotDataView.notDataView(this, rvNotice, refreshLayout);
        pagehttp = 1;
        getListData();
    }

    @Override
    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    /**
     * 获取数据。刷新
     */
    public void getListData() {
        mPresenter.onCommentListData(LoginHelper.getLonginData().getResult(), pagehttp, pageNum, id, type);

    }

    private void initCheckData(int index) {
        switch (index) {
            case 0:
                tvBtnAll.getDelegate().setBackgroundColor(getResources().getColor(R.color.main_color));
                tvBtnHao.getDelegate().setBackgroundColor(getResources().getColor(R.color.text_dddddd));
                tvBtnZhong.getDelegate().setBackgroundColor(getResources().getColor(R.color.text_dddddd));
                tvBtnCha.getDelegate().setBackgroundColor(getResources().getColor(R.color.text_dddddd));
                break;
            case 1:
                tvBtnAll.getDelegate().setBackgroundColor(getResources().getColor(R.color.text_dddddd));
                tvBtnHao.getDelegate().setBackgroundColor(getResources().getColor(R.color.main_color));
                tvBtnZhong.getDelegate().setBackgroundColor(getResources().getColor(R.color.text_dddddd));
                tvBtnCha.getDelegate().setBackgroundColor(getResources().getColor(R.color.text_dddddd));
                break;
            case 2:
                tvBtnAll.getDelegate().setBackgroundColor(getResources().getColor(R.color.text_dddddd));
                tvBtnHao.getDelegate().setBackgroundColor(getResources().getColor(R.color.text_dddddd));
                tvBtnZhong.getDelegate().setBackgroundColor(getResources().getColor(R.color.main_color));
                tvBtnCha.getDelegate().setBackgroundColor(getResources().getColor(R.color.text_dddddd));
                break;
            case 3:
                tvBtnAll.getDelegate().setBackgroundColor(getResources().getColor(R.color.text_dddddd));
                tvBtnHao.getDelegate().setBackgroundColor(getResources().getColor(R.color.text_dddddd));
                tvBtnZhong.getDelegate().setBackgroundColor(getResources().getColor(R.color.text_dddddd));
                tvBtnCha.getDelegate().setBackgroundColor(getResources().getColor(R.color.main_color));
                break;
        }
    }

    @OnClick({R.id.tv_btn_all, R.id.tv_btn_hao, R.id.tv_btn_zhong, R.id.tv_btn_cha})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_btn_all:
                initCheckData(0);
                type = "";
                refreshLayout.autoRefresh();
                break;
            case R.id.tv_btn_hao:
                initCheckData(1);
                type = "1";
                refreshLayout.autoRefresh();
                break;
            case R.id.tv_btn_zhong:
                initCheckData(2);
                type = "2";
                refreshLayout.autoRefresh();
                break;
            case R.id.tv_btn_cha:
                initCheckData(3);
                type = "3";
                refreshLayout.autoRefresh();
                break;
        }
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        RxToast.error(msg);
    }


    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onCommentListSuccess(CommodityCommentListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            } else {
                mAdapter.replaceData(bean.getResult().getData());
                mAdapter.setEmptyView(notDataView);
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }
    }

}
